import {get,update,insert} from '@/api/common'

export default {
  // 返回酒店列表，
  getHotels (areaValue,name,addr,cb,errCb) {
    let body = {
            '@column': 'id,name,addr,conn_person,conn_phone,room_num,checkin_num,checked,checkin_area,remark',
            '@order': 'createdate',
            };
      if(areaValue>0)
        body['checkin_area'] = areaValue;
      if(name!='')
        body['name$'] = '%'+name+'%';
      if(addr!='')
        body['addr$'] = '%'+addr+'%';
      let params = {'[]': {
          'Wh_hotel': body,
          'Dicts':{
            '@column': 'value,name',
            'value@': '/Wh_hotel/checkin_area',
            'object': 'wh_hotel',
            'field': 'checkin_area'
          }
        }
      };
      get(params)
      .then( res=> {
        if(res.data.code==200){
          var data = res.data["[]"];
          var items = []
          for(var d in data) {
            var h = data[d].Wh_hotel;
            var d = data[d].Dicts;
            h['Dicts'] = d==undefined?{}:d;
            items.push(h)
          }
          cb(items)
        }
        else
          errCb('读取申请记录失败'); 
        })
      .catch(function (err){
        console.log('error:'+err);
      });
  },
  // 修改酒店可入住房间数量
  editCheckinNum (id, checkinNum, cb, errCb) {
      let params = {
        'Wh_hotel': {
          'id': id,
          'checkin_num': checkinNum,
        },
        'tag': 'Wh_hotel'
      }
      update(params)
        .then( res=> {
          var b = res.data.code==200&&res.data.msg=='success'
          b?cb():errCb()
        })
        .catch(function (err){
          console.log('error:'+err);
        });
  },
  // 添加或修改酒店信息
  editHotel (hotel, cb, errCb) {
      let params = {
        'Wh_hotel': hotel,
        'tag': 'Wh_hotel'
      }
      let fun = null
      if(hotel.id>0)
        fun = update
      else
        fun = insert
      fun('Wh_hotel',hotel,cb,errCb)
  },
  // 获得医院按首字母分组的列表
  getHospitalIndexBars(cb,errCb) {
    let params = {
        "func()": "getGroupConcatLists(main,wh_hospital,hospital_name,abbr,region_id)"
      };
    get(params,
      data => {
        let list = JSON.parse(data.func).list;
        // 把数据转换成IndexBar可以直接使用的对象{A:[{name:'',id:0,fid:0}...]}
        let firstName = {}
        list.forEach(item=>{
          firstName[item.abbr] = [];
          let names = item.names.split(',')
          let ids = item.ids.split(',')
          let filters = item.filters;
          if(filters!=undefined)
            filters = filters.split(',')
            
          names.forEach((name,index)=>{
              if(filters!=undefined)
                firstName[item.abbr].push({name:name,id:ids[index],fid:filters[index]})
              else
                firstName[item.abbr].push({name:name,id:ids[index]})
          })
        })
        console.log('getHospitalIndexBars')
        cb(firstName)
      },
      err => {errCb(err)}
    )
  },
  // 获得所有医院列表
  getHospitals(cb, errCb) {
    let params = {
      'Wh_hospital[]': {
        'Wh_hospital':{
          '@column': 'id,region_id,hospital_name:name'
        }
      }
    }
    get(params,
      data => {cb(data['Wh_hospital[]'])}, 
      errCb);
  }
}
